/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;

/**
 *
 * @author Muhammad Anis
 */
public class room {
    //memiliki beberapa user dengan chat masing2
    List<String> userinroom = new ArrayList<String>();
    //daftar urutan chatting <username,isi chat>
    List<pairClass> userandchat = new LinkedList<pairClass>();
    //daftar ban
    List<String> bannedUser = new LinkedList<String>();
    //memiliki nama
    public String nama;
    //memiliki moderator
    String moderator;
    //memiliki persistence
    boolean isPersistence;
    //memiliki variable update
    boolean isUpdate;

    public room(String nama,String moderator){
        this.nama = nama;
        this.moderator = moderator;
        this.isPersistence = false;
        userinroom.add(moderator);
        this.isUpdate = false;
    }

    public void addUser(String username){
        if(!userinroom.contains(username) && !bannedUser.contains(username))
            userinroom.add(username);
    }

    public void addChat(String username,String message){
        if(userinroom.contains(username) && !bannedUser.contains(username)){
            userandchat.add(new pairClass(username, message));
            this.isUpdate = true;
        }
    }

    public void printChat(JspWriter out){
        for(pairClass pc :userandchat){
            try {
                out.println(pc.nama + " : " + pc.message + "<br/>");
            } catch (IOException ex) {
                Logger.getLogger(room.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void print(JspWriter out){
        try {
            out.println("<div onclick=\"addChatTab('" + this.nama + "','room');\" class=\"room\">");
                out.println(this.nama);
            out.println("</div>");
        } catch (IOException ex) {
            Logger.getLogger(room.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean isUpdate(){
        boolean temp = this.isUpdate;
        if(temp)
            this.isUpdate = !temp;
        return temp;

    }

    public void setUpdate(boolean update){
        this.isUpdate = update;
    }
}
